package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.domain.Intervention;
import com.itheima.mapper.InterventionMapper;
import com.itheima.service.InterventionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    健康干预服务层实现类
 */
@Service(interfaceClass = InterventionService.class)
@Transactional
public class InterventionServiceImpl implements InterventionService {

    //注入持久层接口
    @Autowired
    private InterventionMapper interventionMapper;


    //添加健康建议
    public void add(Integer[] foodIds, Integer[] sportIds, Integer interventionId, String suggestion) {

        //遍历sportsIds,绑定关联关系
        for (Integer sportId : sportIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("interventionId",interventionId);
            map.put("sportId",sportId);
            interventionMapper.setInterventionAndSports(map);
        }

        //遍历foodIds,绑定关联关系
        for (Integer foodId : foodIds) {
            Map<String,Integer> map2 = new HashMap<>();
            map2.put("interventionId",interventionId);
            map2.put("foodId",foodId);
            interventionMapper.setInterventionAndFood(map2);
        }

        //调用健康师持久层接口
        interventionMapper.add(suggestion);
    }

    @Override
    public List<Integer> findFoodIdsByOrderId(Integer orderId) {
        return null;
    }


    //添加一条医师建议
    public void adviseAdd(Intervention intervention, Integer[] foodIds, Integer[] sportIds) {
        interventionMapper.adviseAdd(intervention);

        Integer interventionId = intervention.getId();

        setInterventionAndFood(interventionId,foodIds);

        setInterventionAndSports(interventionId,sportIds);
    }

    @Override
    public Intervention findByOrderId(Integer id) {
        return null;
    }

    //遍历sportsIds,绑定关联关系,封装方法
    private void setInterventionAndSports(Integer interventionId, Integer[] sportIds) {
        if (sportIds!=null&&sportIds.length>0){
            for (Integer sportId : sportIds) {
                Map map = new HashMap<>();
                map.put("interventionId",interventionId);
                map.put("sportId",sportId);
                interventionMapper.setInterventionAndSports(map);
            }
        }
    }

    //遍历foodIds,绑定关联关系,封装方法
    private void setInterventionAndFood(Integer interventionId, Integer[] foodIds) {
        if (foodIds!=null&&foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map map = new HashMap<>();
                map.put("interventionId",interventionId);
                map.put("foodId",foodId);
                interventionMapper.setInterventionAndFood(map);
            }
        }
    }
}
